//
//  BKCMGetDataFromNetwork.m
//  bestkeep
//
//  Created by 毛韶谦 on 2017/1/16.
//  Copyright © 2017年 utouu. All rights reserved.
//

#import "BKCMGetDataFromNetwork.h"
#import "BKSCCommon.h"
#import "BKCMTransformModel.h"

//购物车商品数量接口
NSString * const strShoping_car_amount = @"shopping-cart/amount";
//购物车列表
NSString * const strShoping_car_list = @"shopping-cart/list";
//购物车推荐列表
NSString *const  strShoping_CarRecommendGoodsList = @"getRecommendGoodsList";
//购物车商品修改接口
NSString * const strShoping_car_edit = @"shopping-cart/edit";
//购物车商品删除接口
NSString * const strShoping_car_delete = @"shopping-cart/delete";
//添加收藏商品接口
NSString * const strShoping_collect_add = @"collect/add";
//购物车检验接口app购物车提交前预校验
NSString *const strShoping_preConfirm = @"order/pre-confirm";


@implementation BKCMGetDataFromNetwork


//获取购物车商品数量
+(void)GetBuyCarAmountSuccess:(successBlock)success ErrorMessage:(void(^)(id obj))errorMessage failure:(void(^)(NSError *error))failure{
    NSString *strUrl = BKSCURL(strShoping_car_amount);
    [[RequestManager manager]bkDataRequest:RequestMethodPost URLString:strUrl parameters:nil success:^(id  _Nullable responseObject) {
        
        if(!responseObject) {
            return;
        }
        if (![[responseObject objectForKey:@"success"] boolValue]){
            
            errorMessage(responseObject[@"msg"]);
            return ;
        }
        NSString *amount = responseObject[@"data"][@"amount"];
        success(amount);
        
    } failure:^(id  _Nullable responseObject, NSError * _Nullable error) {
        
        failure(error);
    }];
}

//购物车模型
+(void)GetBuycarDataFromJsonSuccess:(void (^)(NSArray *))sucess failure:(failure)failure{
    
    NSString *urlStr = BKSCURL(strShoping_car_list);
    [[RequestManager manager]bkDataRequest:RequestMethodPost URLString:urlStr parameters:nil success:^(id  _Nullable responseObject) {
        
        BOOL success = [[responseObject objectForKey:@"success"] boolValue];
        if (!success){
            
            failure([responseObject objectForKey:@"code"]);
        }else {
            sucess([BKCMTransformModel dictionaryTransformModel:responseObject]);
        }
    } failure:^(id  _Nullable responseObject, NSError * _Nullable error) {
        
            failure([NSString stringWithFormat:@"%ld",error.code]);
    }];
}
//购物车推荐数组模型

+(void)GetBuycarRecommendGoodsDataFromJsonSucessWithParamer:(NSString*)currentPage Success:(successBlock)sucess failure:(failure)failure {
    NSString *urlStr = BKSCURL(strShoping_CarRecommendGoodsList);
    
    NSMutableDictionary *body_dic = [NSMutableDictionary dictionaryWithObjectsAndKeys:currentPage,@"currentPage", nil];
    [[RequestManager manager] bkDataRequest:RequestMethodPost URLString:urlStr parameters:body_dic success:^(id  _Nullable responseObject) {
        
        BOOL success = [[responseObject objectForKey:@"success"] boolValue];
        if (!success){
            failure([responseObject objectForKey:@"msg"]);
            [HUD showMessage:[responseObject objectForKey:@"msg"]];
            return ;
        }else {
            
            sucess([BKCMTransformModel buyCarRecommendGoodsDataDictionaryTransformModel:responseObject]);
        }
        
    } failure:^(id  _Nullable responseObject, NSError * _Nullable error) {
            failure([NSString stringWithFormat:@"%d",error.code]);
    }];
}

//修改商品信息
+(void)editBuyCarGoodsFromJson:(NSString *)str success:(successBlock)success failure:(void(^)(NSError *error))failure{
    NSString *strUrl = BKSCURL(strShoping_car_edit);
    
    NSDictionary *body_dic = [NSDictionary dictionaryWithObjectsAndKeys:str,@"data", nil];
    
    [[RequestManager manager] bkDataRequest:RequestMethodPost URLString:strUrl parameters:body_dic success:^(id  _Nullable responseObject) {
        
        NSDictionary *dic = responseObject;
        if ([dic[@"success"] boolValue]) {
            success(responseObject);
        }else {
            [HUD showMessage:dic[@"msg"]];
        }
    } failure:^(id  _Nullable responseObject, NSError * _Nullable error) {
        if (error.code==502) {
            [HUD showMessage:@"服务器开小差了，请稍后再试！"];
        }else{
            [HUD showMessage:error.localizedDescription];
        }

    }];
}
//删除商品信息
+(void)deleteBuyCarGoods:(NSString *)str success:(successBlock)success failure :(void(^)(NSError *error))failure{
    //    NSString *strUrl = @"http://api.test.bestkeep.cn/shopping-cart/delete";
    NSString *strUrl = BKSCURL(strShoping_car_delete);
    
    NSDictionary *body_dic = [NSDictionary dictionaryWithObjectsAndKeys:str,@"data", nil];
    
    [[RequestManager manager]bkDataRequest:RequestMethodPost URLString:strUrl parameters:body_dic success:^(id  _Nullable responseObject) {
        
        NSDictionary *dic = responseObject;
        if ([dic[@"success"] boolValue]) {
            success(responseObject);
        }else {
            [HUD showMessage:dic[@"msg"]];
            failure(nil);
        }
    } failure:^(id  _Nullable responseObject, NSError * _Nullable error) {
        if (error.code==502) {
            [HUD showMessage:@"服务器开小差了，请稍后再试！"];
        }else{
            [HUD showMessage:error.localizedDescription];
        }
        failure(error);
    }];
    
}
//移到收藏夹
+(void)moveToSaveBuyCarGoods:(NSString *)str success:(successBlock)success failure:(void(^)(NSError *error))failure{
    
    NSString *strUrl = BKSCURL(strShoping_collect_add);
    
    NSDictionary *body_dic = [NSDictionary dictionaryWithObjectsAndKeys:str,@"data", nil];
    
    [[RequestManager manager]bkDataRequest:RequestMethodPost URLString:strUrl parameters:body_dic success:^(id  _Nullable responseObject) {
        
        NSDictionary *dic = responseObject;
        if ([dic[@"success"] boolValue]) {
            success(responseObject);
        }else {
            [HUD showMessage:dic[@"msg"]];
            failure(nil);
        }
    } failure:^(id  _Nullable responseObject, NSError * _Nullable error) {
        if (error.code==502) {
            [HUD showMessage:@"服务器开小差了，请稍后再试！"];
        }else{
            [HUD showMessage:error.localizedDescription];
        }
        failure(error);
    }];
    
}

/*
 购物车结算前的校验
 */
+(void)SettlementCheckBuyCarGoods:(NSString *)str returnInformation:(successBlock)returnInformation failure:(void(^)(NSError *error))failure{
    
    NSString *strUrl = BKSCURL(strShoping_preConfirm);
    
    NSDictionary *body_dic = [NSDictionary dictionaryWithObjectsAndKeys:str,@"data", nil];
    
    [[RequestManager manager] bkDataRequest:RequestMethodPost URLString:strUrl parameters:body_dic success:^(id  _Nullable responseObject) {
        
        NSDictionary *dic = responseObject;
        if ([dic[@"success"] boolValue]) {
            returnInformation(dic);
        }else {
            [HUD showMessage:dic[@"errorMsg"]];
            failure(nil);
        }
        
    } failure:^(id  _Nullable responseObject, NSError * _Nullable error) {
        
        if (error.code==502) {
            [HUD showMessage:@"服务器开小差了，请稍后再试！"];
        }else{
            [HUD showMessage:error.localizedDescription];
        }
        failure(error);
    }];
    
}


@end
